package com.zkzx.company.base.controller;

import com.zkzx.common.company.command.CompanyCommand;
import com.zkzx.common.company.model.CompanyListVO;
import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.company.param.CompanyFilterParam;
import com.zkzx.common.exception.BaseException;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.common.vo.StatusVO;
import com.zkzx.company.base.service.CompanyService;
import com.zkzx.util.json.JSONResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月28日 17时51分
 */
@RestController
@Validated
@Api( tags="企业管理")
@RequestMapping(value = "/companies")
public class CompanyBaseController extends BaseController {

    @Autowired
    private CompanyService companyService;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取企业列表", notes = "获取企业列表")
    public PageResult<CompanyListVO> find(@ApiParam(value = "查询参数") @RequestParam("filterParam") String filterParamJson,
                                          @ApiParam(value = "分页查询参数")  @RequestParam("params") String paramsJson) throws Exception {

        CompanyFilterParam filterParam = (CompanyFilterParam) JSONResultUtil.toObject(filterParamJson, CompanyFilterParam.class);
        RequestFilterParams params = (RequestFilterParams) JSONResultUtil.toObject(paramsJson, RequestFilterParams.class);
        return companyService.find(filterParam,params);
    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取企业详细", notes = "获取企业详细")
    public CompanyVO getById(@PathVariable("id") Integer id) {
        return companyService.getById(id);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = "新增企业信息", notes = "新增企业信息")
    public StatusVO add(@RequestBody @Valid CompanyCommand command) throws BaseException {
        return companyService.add(command,super.getUserId());
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改企业信息", notes = "修改企业信息")
    public void update(@PathVariable("id") Integer id,@RequestBody @Valid CompanyCommand command) {
        companyService.update(command,id,super.getUserId());
    }

    @PostMapping("/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = "提交企业信息", notes = "提交企业信息")
    public StatusVO submit(@PathVariable("id") Integer id) throws BaseException {
        return companyService.submit(id,super.getUserId());
    }



}